package com.thwj.project.app.convert;

import com.thwj.project.app.api.vo.MerchantTagItemVO;
import com.thwj.project.app.api.vo.MerchantTagVO;
import com.thwj.project.biz.enums.MerchantTagEnum;
import com.thwj.project.biz.enums.MerchantTagTypeEnum;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MerchantConvert {

    public static List<MerchantTagVO> convertTagList(final String tagIdListStr) {
        List<MerchantTagVO> tagVOList = new ArrayList<>();
        for (MerchantTagTypeEnum value : MerchantTagTypeEnum.values()) {
            MerchantTagVO tagVO = new MerchantTagVO();
            tagVO.setClassName(value.getName());
            List<MerchantTagItemVO> tagItemVOList = new ArrayList<>();
            tagVO.setTagItemVOList(tagItemVOList);
            tagVOList.add(tagVO);
            for (MerchantTagEnum merchantTagEnum : MerchantTagEnum.values()) {
                if (value.equals(merchantTagEnum.getTypeEnum())) {
                    MerchantTagItemVO tagItemVO = new MerchantTagItemVO();
                    tagItemVO.setName(merchantTagEnum.getName());
                    tagItemVO.setValue(merchantTagEnum.getValue());
                    tagItemVOList.add(tagItemVO);
                    if(tagIdListStr != null) {
                        // 判断当前标签是否已选择
                        List<String> tagIdList = Arrays.asList(tagIdListStr.split(","));
                        if(tagIdList.contains(merchantTagEnum.getValue().toString())) {
                            tagItemVO.setUserSelected(true);
                        }
                    }
                }
            }
        }
        return tagVOList;
    }

    public static List<MerchantTagVO> convertOnlyShowTagList(final String tagIdListStr) {
        List<MerchantTagVO> tagVOList = new ArrayList<>();
        for (MerchantTagTypeEnum value : MerchantTagTypeEnum.values()) {
            MerchantTagVO tagVO = new MerchantTagVO();
            tagVO.setClassName(value.getName());
            List<MerchantTagItemVO> tagItemVOList = new ArrayList<>();
            tagVO.setTagItemVOList(tagItemVOList);

            for (MerchantTagEnum merchantTagEnum : MerchantTagEnum.values()) {
                if (value.equals(merchantTagEnum.getTypeEnum())) {
                    MerchantTagItemVO tagItemVO = new MerchantTagItemVO();
                    tagItemVO.setName(merchantTagEnum.getName());
                    tagItemVO.setValue(merchantTagEnum.getValue());
                    if(tagIdListStr != null) {
                        // 判断当前标签是否已选择
                        List<String> tagIdList = Arrays.asList(tagIdListStr.split(","));
                        if(tagIdList.contains(merchantTagEnum.getValue().toString())) {
                            tagItemVO.setUserSelected(true);

                            tagItemVOList.add(tagItemVO);
                        }
                    }
                }
            }

            if(!CollectionUtils.isEmpty(tagItemVOList)) {
                tagVOList.add(tagVO);
            }
        }
        return tagVOList;
    }
}
